/**
 *  *2010-4-10 下午08:51:41  add 
 */
package com.base.myproject.client.busi.system;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.fileupload.IUploader;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.SingleFileUpload;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Label;

/**
 * 业务编码
 * 
 * @author Administrator 2010-4-10 javazgw@gmail.com
 */
public class Business extends BaseJDBCPanel {

	com.extjs.gxt.ui.client.widget.FramePanel frame_flow = com.extjs.gxt.ui.client.widget.FramePanel
			.pop();

	public Business() {
		super();
		init();
	}

	public void init() {

		initWidget();
		initbutton();
		showflowImage();
		this.registerMainStore(store);
		this.registerStore(store);
	}

	private void initbutton() {
		// TODO Auto-generated method stub

	}

	private void initWidget() {

		// this.setSize(400, 800);

		// ContentPanel cp = new ContentPanel();
		// cp.setLayout(new RowLayout(Orientation.VERTICAL));
		// cp.add(createGrid(),new RowData(1,0.5));
		// cp.add(createForm(),new RowData(1,-1));
		// cp.setHeight(300);
		HorizontalPanel h = new HorizontalPanel();
		h.setVerticalAlign(VerticalAlignment.TOP);
		VerticalPanel v = new VerticalPanel();
		v.add(createGrid());
		v.add(createForm());

		h.add(v);
		h.add(frame_flow);

		// v.setWidth(1500);
		// v.setAutoWidth(true);
		// RootPanel.get("zgwtest").add(createGrid());

		this.add(h);
		// this.setAutoHeight(true);

	}

	public void showflowImage() {
		store.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED,
				new BaseStoreListener() {

					@Override
					public void BaseStoreEventHandle(BaseStoreEvent evt) {
						// TODO Auto-generated method stub
						System.out.println(evt.getBasestore().getValue(
								"busicode"));

						// frame_flow.setSize("300", "300");
						frame_flow.el().setElementAttribute(
								"src",
								"./busijsp/ratify/getratifyimage.jsp?busicode="
										+ evt.getBasestore().getValue(
												"busicode"));
						frame_flow.el().setElementAttribute("height", "400px");
						frame_flow.el().setElementAttribute("width", "400px");
					}

					@Override
					public void onBrowserEvent(Event event) {
						// TODO Auto-generated method stub

					}

				});
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Integer.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("businame");
		type.getField("businame").setType(String.class);
		type.addField("ratifyicode");
		type.getField("ratifyicode").setType(String.class);
		type.addField("scripttext");
		type.getField("scripttext").setType(String.class);
		type.addField("imagepath");
		type.getField("imagepath").setType(String.class);
		type.addField("xmlpath");
		type.getField("xmlpath").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("business");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select icode,busicode,businame,ratifyicode,scripttext,imagepath,xmlpath from business");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGridPanel gridpanel = null;

	BaseGridPanel createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("busicode", "业务编码", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("businame", "业务名称", 80);
		columns.add(C2);
		// ColumnConfig C3 = new ColumnConfig("ratifyicode", "ratifyicode", 80);
		// columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("scripttext", "脚本", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("imagepath", "审批图片路径", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("xmlpath", "审批流程文件", 80);
		columns.add(C6);

		ColumnModel cm = new ColumnModel(columns);
		gridpanel = new BaseGridPanel(getStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		gridpanel.getBg().setSelectionModel(cbs);
		gridpanel.setHeight(200);
		gridpanel.setWidth(536);
		gridpanel.getBg().getView().setForceFit(true);
		// 1046138117
		return gridpanel;
	}

	BaseFormPanel form = null;
	TextField<String> C_num1 = null;
	TextField<String> C_num2 = null;
	
	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore());
		TableLayout tl = new TableLayout(4);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		TableData td2 = new TableData();
		td2.setRowspan(1);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);
		td2.setColspan(3);

		TableData td3 = new TableData();
		td3.setRowspan(1);
		td3.setHorizontalAlign(HorizontalAlignment.LEFT);
		td3.setVerticalAlign(VerticalAlignment.TOP);
		td3.setPadding(2);
		td3.setColspan(3);

		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("业务编码:"), td);
		C_num1 = new TextField<String>();
		C_num1.setName("busicode");
		form.add(C_num1, td);
		C_num1.addListener(Events.OnClick, getListener());
		form.add(new Label("业务名称:"), td);
		C_num2 = new TextField<String>();
		C_num2.setName("businame");
		form.add(C_num2, td);
		C_num2.addListener(Events.OnClick, getListener());
		// form.add(new Label("ratifyicode:"), td);
		// TextField<String> C_num3 = new TextField<String>();
		// C_num3.setName("ratifyicode");
		// form.add(C_num3, td);

		form.add(new Label("审批图片路径:"), td);
		TextField<String> C_num5 = new TextField<String>();
		C_num5.setName("imagepath");
		form.add(C_num5, td);

		form.add(new Label("审批流程文件:"), td);
		TextField<String> C_num6 = new TextField<String>();
		C_num6.setName("xmlpath");
		form.add(C_num6, td);

		form.add(new Label("脚本:"), td);
		TextArea C_num4 = new TextArea();
		C_num4.setMaxLength(1000);
		C_num4.setWidth(390);
		C_num4.setName("scripttext");
		form.add(C_num4, td3);

		form.add(new Label("上传流程图:"), td);
		final SingleFileUpload su = new SingleFileUpload();
		su.setValidExtensions(new String[] { "xml" });
		su.setPrgBarInterval(100000);

		su.setOnStartHandler(new ValueChangeHandler<IUploader>() {

			@Override
			public void onValueChange(ValueChangeEvent<IUploader> event) {

				System.out.println("start=" + su.servletPath);
				su
						.setServletPath("gupld?filestorepath=xmlpath&uploadcommand=mankeflow&icode="
								+ store.getValue("icode")
								+ "&storesql=update business set xmlpath={filepath} where icode="
								+ store.getValue("icode"));
				System.out.println("start=" + su.servletPath);

				int curindex = store.getCurcowindex();
				store.reload();
				// store.gotorow(curindex);
				// gridpanel.getBg().

			}

		});

		su.setOnFinishHandler(new ValueChangeHandler<IUploader>() {

			@Override
			public void onValueChange(ValueChangeEvent<IUploader> event) {

				IUploader uploader = event.getValue();
				System.out.println("uploader=" + uploader);

				uploader = null;

			}

		});
		form.add(su, td2);

		// RootPanel.get("zgwtest").add(su);

		form.inited();
		form.setAutoHeight(true);
		form.setAutoWidth(true);
		return form;
	};

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals("按钮变量")) {

							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("输入型变量")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}else if(be.getType() == Events.OnClick){
							if(be.getSource().equals(C_num1)){
								if(C_num1.getValue().startsWith("80")){
									C_num1.setReadOnly(true);
									C_num2.setReadOnly(true);
								}
							}else if(be.getSource().equals(C_num2)){
								if(C_num2.getValue().endsWith("模块")){
									C_num1.setReadOnly(true);
									C_num2.setReadOnly(true);
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
